package com.snail.billing.json;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonWorkQuery extends JsonBase {

	private int total;
	private List<Problem> problems;

	public JsonWorkQuery(String jsonString) {
		super(jsonString);
		try {
			if (getCode() == 1) {
				JSONObject json = new JSONObject(jsonString);
				if (json.has("total")) {
					total = json.getInt("total");
				}
				JSONArray jsonArray = json.getJSONArray("data");
				// TreeMap<String, Problem> treeMap = new TreeMap<String, Problem>(new Comparator<String>() {
				//
				// public int compare(String o1, String o2) {
				// return o2.compareTo(o1);
				// }
				// });
				// for (int i = 0; i < jsonArray.length(); i++) {
				// Problem problem = new Problem(jsonArray.getString(i));
				// if (!problem.getState().equals("0")) {
				// treeMap.put(problem.getCreateDate(), problem);
				// }
				// }
				problems = new ArrayList<Problem>();
				// for (String key : treeMap.keySet()) {
				// problems.add(treeMap.get(key));
				// }
				for (int i = 0; i < jsonArray.length(); i++) {
					Problem problem = new Problem(jsonArray.getString(i));
					problems.add(problem);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public int getTotal() {
		return total;
	}

	public List<Problem> getProblems() {
		return problems;
	}

	public static class Problem {
		private String id;
		private String createDate;
		private String state;
		private String title;
		private String content;

		public Problem() {
		}

		public Problem(String jsonString) {
			try {
				JSONObject json = new JSONObject(jsonString);
				if (json.has("CWFID")) {
					id = json.getString("CWFID");
				}
				if (json.has("CREATEDATE")) {
					createDate = json.getString("CREATEDATE");
				}
				if (json.has("STATE")) {
					state = json.getString("STATE");
				}
				if (json.has("FORMTITLE")) {
					title = json.getString("FORMTITLE");
				}
				if (json.has("CONTENT")) {
					content = json.getString("CONTENT");
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public String getCreateDate() {
			return createDate;
		}

		public void setCreateDate(String createDate) {
			this.createDate = createDate;
		}

		public String getState() {
			return state;
		}

		public void setState(String state) {
			this.state = state;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getContent() {
			return content;
		}

		public void setContent(String content) {
			this.content = content;
		}

	}

}
